package com.hc.contorl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hc.pojo.TbUser;
import com.hc.service.TbUserService;
import com.hc.service.impl.TbUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;

@WebServlet(value = "/userDetail")
public class UserDetailContorller extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String id=req.getParameter("id");
        TbUserService service=new TbUserServiceImpl();
        TbUser user=service.findById(Long.parseLong(id));
        /*//因为前台是用ajax的json格式来进行数据传输的，所以需要把查询出来的结果也转换为json的格式返回给前台
        ObjectMapper mapper = new ObjectMapper();

        //这里我们用jackson的jar包来，把查询出的对象转换成json字符串，来返回给前台
        String json = mapper.writeValueAsString(tbUserJson);

        //最后将结果输出给前台
        PrintWriter out = resp.getWriter();
        out.print(json);
        out.flush();
        out.close();*/
        ObjectMapper mapper=new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        String json=mapper.writeValueAsString(user);
        //      System.out.println(json);
        PrintWriter out=resp.getWriter();
        out.print(json);
        //输出流清空缓存的方法，作用是将读取的json文件立即输出
        out.flush();
        out.close();
    }
}
